package com.aner.test.controller;

import com.aner.test.domain.CommonResult;
import com.aner.test.domain.TestObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * Patch method test controller
 *
 * @author aner
 * @version 1.0
 * @date 2020/7/23 10:29
 */
@RestController
@RequestMapping("")
public class PatchTestController {

    Logger logger = LoggerFactory.getLogger(PatchTestController.class);

    @PatchMapping("/testPathVariable/{params}/{id}")
    public CommonResult  testPathVariable(@PathVariable List<String> params, @PathVariable Long[] id){

        List<String> test = params.stream().map(ele -> {
            try {
                String str = new String (ele.getBytes("iso-8859-1"),"UTF-8");
                return str;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return ele;
            }
        }).collect(Collectors.toList());
        logger.info("test: {}; id = {}", test, id);
        CommonResult result = CommonResult.success();
        result.put("test", test);
        return result;
    }

    @PatchMapping("/testRequestParamList/")
    public CommonResult  testRequestParamList(@RequestParam List<String> testList){
        logger.info("testRequestParamList");
        return CommonResult.success(testList);
    }


    @PatchMapping("/testRequestParamDate/")
    public CommonResult  testRequestParamDate(@RequestParam Date testDate){
        logger.info("testRequestParamDate");
        return CommonResult.success(testDate);
    }


    @PatchMapping("/testRequestBody/")
    public CommonResult  testBody(@RequestBody TestObject testObject){
        logger.info("testBody");
        return CommonResult.success(testObject);
    }

    @PatchMapping("/testRequestBodyMap/")
    public CommonResult  testRequestBodyMap(@RequestBody Map<String, Object> testMap){
        logger.info("testRequestBodyMap");
        return CommonResult.success(testMap);
    }

    @PatchMapping("/testRequestBodyList/")
    public CommonResult  testRequestBodyList(@RequestBody List<String> testList){
        logger.info("testRequestBodyList");
        return CommonResult.success(testList);
    }

    @PatchMapping("/testRequestBodyDate/")
    public CommonResult  testRequestBodyDate(@RequestBody Date test){
        logger.info("testRequestBodyDate");
        return CommonResult.success(test);
    }



    @PatchMapping("/testRequestBodyLong/")
    public CommonResult  testRequestBodyLong(@RequestBody Long test){
        logger.info("testRequestBodyLong");
        return CommonResult.success(test);
    }

    @PatchMapping("/test/")
    public CommonResult  test(TestObject testObject){
        logger.info("test");
        return CommonResult.success(testObject);
    }

    @PatchMapping("/testDate/")
    public CommonResult  testDate(Date test){
        logger.info("testDate");
        return CommonResult.success(test);
    }
}
